package com.bluefish.bltuan.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bluefish.bltuan.dao.CategoryDao;
import com.bluefish.bltuan.module.Category;
import com.bluefish.bltuan.service.CategoryService;
import com.bluefish.bltuan.util.PageIndex;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService{

	private CategoryDao categoryDao;
	
	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public void save(Category t) {
		this.categoryDao.save(t);
	}

	public void update(Category t) {
		this.categoryDao.update(t);
	}

	public void delete(Category t) {
		this.categoryDao.delete(t);
	}

	public Category findById(Integer id) {
		return this.categoryDao.findById(id);
	}

	public List<Category> listCategoryByType(int type) {
		return this.categoryDao.getCategoryByType(type);
	}
	
	public List<Category> listCities() {
		return listCategoryByType(CITY);
	}
	
	public List<Category> listStartedCities() {
		return this.categoryDao.getStartedCategoryByType(CITY);
	}

	public void addCity(Category city) {
		Category parent = this.categoryDao.getParentByType(CITY);
		parent.addChild(city);
		this.categoryDao.update(parent);
	}
	
	public void closeCity(Category currentCity) {
		currentCity.setStart(false);
		this.categoryDao.update(currentCity);
	}
	
	public void startCity(Category currentCity) {
		currentCity.setStart(true);
		this.categoryDao.update(currentCity);
	}

	public List<Category> listCities(PageIndex pageIndex) {
		return this.categoryDao.list(pageIndex, CITY);
	}

	public Category getCityByName(String cityName) {
		List<Category> cities = this.categoryDao.listByCityName(cityName);
		return cities != null && cities.size() > 0 ? cities.get(0) : null;
	}

	public Category getCityByNickName(String nickname) {
		List<Category> cities = this.categoryDao.listByCityNickName(nickname);
		return cities != null && cities.size() > 0 ? cities.get(0) : null;
	}

}
